using System;
using System.Collections.Generic;
using System.Text;

using gudusoft.gsqlparser;
using gudusoft.gsqlparser.Units;


namespace tablefieldrel
{
    class tablefieldrel
    {
        static void Main(string[] args)
        {
            int c = Environment.TickCount;

            // create a sql parser instance supports ms sql server sql dialect
            TGSqlParser sqlparser = new TGSqlParser(TDbVendor.DbVMssql);
            Console.WriteLine("Time escaped after starting: {0}", Environment.TickCount - c);

            String var1 = "SELECT   a.Id,";
            var1 = var1 + "          a.Name      AS Surname,";
            var1 = var1 + "          b.preName,";
            var1 = var1 + "          c.Busrel_Id";
            var1 = var1 + " FROM     Customer a";
            var1 = var1 + "          LEFT JOIN CustomerDetail b";
            var1 = var1 + "            ON a.Id = b.fId";
            var1 = var1 + "          LEFT JOIN Businessrel c";
            var1 = var1 + "            ON b.cId = c.Bid";
            var1 = var1 + "          JOIN Contract d";
            var1 = var1 + "            ON c.Id = d.fId";
            var1 = var1 + "               AND a.DATE < '20071204:13.23.14.000'";
            var1 = var1 + "               AND a.cst_Class_cd IN ('01','02')";
            var1 = var1 + "               AND b.Name LIKE 'W%'";
            var1 = var1 + "               AND (SELECT MAX(e.TIMESTAMP)";
            var1 = var1 + "                    FROM   dDate e";
            var1 = var1 + "                    WHERE  e.colLid = a.Id)";
            var1 = var1 + " ORDER BY a.Name,";
            var1 = var1 + "          b.preName";

            sqlparser.SqlText.Text = var1;
            int iRet = sqlparser.Parse();
            Console.WriteLine("Time escaped after parsing: {0}", Environment.TickCount - c);

            if (iRet == 0)
            {

                string lctablename = "NULL";
                string lcfieldname = "";
                string lcfieldalias = "NULL";

                TSelectSqlStatement stmt = (TSelectSqlStatement)sqlparser.SqlStatements[0];
                foreach (TLzField lcfield in stmt.Fields)
                {
                    lctablename = "NULL";
                    lcfieldalias = "NULL";

                    if (lcfield.FieldPrefix.Length > 0)
                    {
                        lctablename = findtablename(stmt.Tables,lcfield.FieldPrefix);
                    }

                    lcfieldname = lcfield.FieldName;

                    if (lcfield.FieldAlias.Length > 0)
                    {
                      lcfieldalias = lcfield.FieldAlias;
                    }
                    
                    Console.WriteLine("{0},{1},{2}",lctablename,lcfieldname,lcfieldalias);
                }
            }
            else
            {
                Console.WriteLine("Syntax error found in input sql:");
                Console.WriteLine(sqlparser.ErrorMessages);

            }

        }
        public static string findtablename(TLzTableList tbl, string tname)
        {
            foreach (TLzTable tb in tbl)
            {
                if (tb.TableName.CompareTo(tname) == 0)
                {
                    return tb.TableName;
                }
                else if (tb.TableAlias.CompareTo(tname) == 0)
                {
                    return tb.TableName;
                }
            }
            return tname;
        }
    }
}
